package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;

import com.itheima.exception.CheckItemException;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
@Autowired
    private CheckItemMapper checkItemMapper;
    @Override
    public void add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //告诉mybatis要分页
        com.github.pagehelper.Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //写一个普普通通的查询sql跟分页毫无关系
         List<CheckItem> checkItems=checkItemMapper.findPage(queryPageBean.getQueryString());
         //上面两行位置不能换
        return new PageResult(page.getTotal(),checkItems);
    }

    @Override
    public void delete(Integer id) throws CheckItemException {
        Integer count=checkItemMapper.findCountByCheckItemId(id);
        if(count>0){
            //可以抛异常
            throw new CheckItemException("存在引用不能删除");
        }else{
            checkItemMapper.delete(id);
        }
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemMapper.findById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemMapper.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.findAll();
    }

}
